define("login", ["jquery", "jquery-validate", "jquery.idcode", "jquery.cookie"], function () {
    return {
        login: function () {
            $.idcode.setCode()
            $("#idcode_change").on("click", function () {
                $.idcode.setCode()
            })
            $("form").validate({
                rules: {
                    telphone: {
                        required: true,
                    },
                    upwd: {
                        required: true,
                    },
                    Txtidcode: {
                        required: true
                    }
                },
                messages: {
                    telphone: {
                        required: "手机号码不能为空",
                    },
                    upwd: {
                        required: "密码不能为空",
                    },
                    Txtidcode: {
                        required: ""
                    }
                },
                submitHandler: function () {
                    var formdata = {
                        telphone: $("form")[0].telphone.value,
                        upwd: $("form")[0].upwd.value,
                    }
                    console.log(formdata);
                    $.ajax({
                        url: "http://127.0.0.1/mailegou/server/login.php",
                        data: formdata,
                        type: "post",
                    }).then(function (res) {
                        console.log(res);
                        $.cookie("status", res.user_id)
                        console.log(JSON.parse($.cookie("cart") || '[]'));
                        var datainfo = JSON.parse($.cookie("cart") || '[]')
                        console.log(datainfo);
                        if($.cookie("cart")){
                            if (datainfo.length != 0) {
                                for (let i=0 ;i<datainfo.length;i++)
                                {
                                    datainfo[i].user_id = res.user_id
                                    console.log(datainfo);
                                    $.ajax({
                                        url: "http://127.0.0.1/mailegou/server/car.php",
                                        data: datainfo[i],
                                        type: "post",
                                    }).then(function (res) {
                                        console.log(res);
                                    })
                                }
                                $.cookie("cart", "", {expires: -1});
                            }
                        }
                        console.log(res);
                        if(res.status==1)
                        {
                            document.location.assign("index.html")
                        }

                    })
                    return false
                }
            })
        }
    }
})